package com.zdb.n1.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.OptBoolean;
import jakarta.persistence.*;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Entity
@Table(name = "health_data")
@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class HealthData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    @JsonIgnoreProperties({"password", "roles"})
    private User user;

    @Column(nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private Date recordDate;

    @Column(nullable = false)
    @Min(value = 50, message = "身高不能小于50厘米")
    @Max(value = 250, message = "身高不能大于250厘米")
    private Double height; // 单位：厘米

    @Column(nullable = false)
    @Min(value = 20, message = "体重不能小于20千克")
    @Max(value = 300, message = "体重不能大于300千克")
    private Double weight; // 单位：千克

    @Column
    @Min(value = 30, message = "静息心率不能小于30次/分钟")
    @Max(value = 200, message = "静息心率不能大于200次/分钟")
    private Integer restingHeartRate; // 静息心率，单位：次/分钟

    @Column
    private Double bmi; // 体重指数，计算得出

    @PrePersist
    @PreUpdate
    public void calculateBMI() {
        if (height != null && weight != null && height > 0) {
            // 将厘米转换为米再计算
            double heightInMeters = height / 100.0;
            bmi = weight / (heightInMeters * heightInMeters);
            // 四舍五入到两位小数
            bmi = Math.round(bmi * 100.0) / 100.0;
        }
    }
} 